<template>
	<view id="barcode"></view>
</template>

<script>
	export default {
		// props: {
		// 	scanTip:{
		// 		type: String,
		// 		default:'',
		// 	}
		// },
		data() {
			return {
				
				// #ifdef APP-PLUS
				barcode: [
					plus.barcode.QR,
					plus.barcode.EAN13,
					plus.barcode.EAN8,
					plus.barcode.UPCA,
					plus.barcode.UPCE,
					plus.barcode.CODABAR,
					plus.barcode.CODE39,
					plus.barcode.CODE93,
					plus.barcode.CODE128,
					plus.barcode.ITF,
				],//码类型
				currentWebview:null,
				content:null,
				closeView:null,
				// #endif
				scanTipNew:this.scanTip,
				textTop:'',
				scanTip:'',
			};
		},
		watch:{
			scanTip(val){
				let that=this;
				this.scanTipNew=val;
				// uni.showToast({
				// 	icon: 'none',
				// 	title: val,
				// 	duration:2000,
				// })
				// console.lgo(9999999999999)
				this.content.reset()
			}
		},
		created() {
			
			var height = uni.getSystemInfoSync().screenHeight;
			var pages = getCurrentPages();
			var page = pages[pages.length - 1];
			this.currentWebview = page.$getAppWebview();
			this.createBarcode(); //创建二维码窗口
			this.createView(); //创建操作按钮及tips界面
		},
		onUnload() {
			clearTimeout(this.t);
		},
		mounted() {
			// #ifdef APP-PLUS
			this.$eventHub.$off('appScanClose');
			this.$eventHub.$on('appScanClose',() => {
				this.close()
			});
			// #endif
		},
		methods: {
			createBarcode() {
					this.barcode = plus.barcode.create('barcode', this.barcode, {//二维码显示框
						top: '0',
						left: '0px',
						width: '100%',
						height: '100%',
						 position: 'static', //静态定位  
						frameColor: '#02FF00',
						scanbarColor: '#02FF00',
					});
					this.barcode.onmarked = this.onmarked;
					// this.barcode.setFlash(this.flash);
					this.currentWebview.append(this.barcode);
					// barcode.start();
					// const res = uni.getSystemInfoSync();
					this.barcode.start();
				},
				// 创建操作按钮及tips
				createView() {
					let that=this;
					// 创建关闭按钮
					this.closeView = new plus.nativeObj.View('closeView', {
							top: '0px',
							left: '0px',
							height: '100%',
							width: '100%'
						},
						[{
							 tag: 'img',
							id: 'scanClose',
							src:'packageC/static/img/close.png',
							position: {
								top: '18%',
								right: '30px',
								width: '20px',
								height: '20px',
								padding:'15px',
								background:'#fff'
							}
						}]);
					// 创建展示类内容组件
					this.content = new plus.nativeObj.View('content', {
							top: '0px',
							left: '0px',
							height: '100%',
							width: '100%'
						},
						[
							{
								tag: 'font',
								id: 'scanTips',
								text: that.scanTipNew,
								textStyles: {
									size: '18px',
									color: '#ffffff',
									whiteSpace: 'normal'
								},
								position: {
									bottom: '60%',
									left: '0',
									width: '100%',
									height: 'wrap_content'
								}
							}
						]);
					this.currentWebview.append(this.content);
					this.currentWebview.append(this.closeView);
					this.closeView.addEventListener("click", function(e) { //关闭按钮
						that.$emit('close');
						that.close();
					}, false);
				},
				close(){
					if(this.currentWebview){
						uni.navigateBack();
						this.barcode.cancel();
						this.barcode.close();
						this.content.close();
						this.closeView.close();
						this.content=null;
						this.closeView=null;
						this.currentWebview=null;
					}
				},
			onmarked(type, result) {
				let that=this;
				// this.$emit('scanCode',result);
				this.$eventHub.$emit('appScanCode',result);
				console.log(result)
				this.t=setTimeout(() => {
					this.barcode.start();
				}, 1000)
			},
			
		}
	}
</script>

<style lang="scss">

</style>
